/**
 * @file serverplayer.h
 * @brief ServerPlayer class definition
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * @author VestniK (Sergey N.Vidyuk) sir.vestnik@gmail.com
 * @date 8 Jun 2008
 */
#ifndef _Player_H_
#define _Player_H_

#include <QtCore/QObject>
#include <QtCore/QList>
#include <QtCore/QString>
#include <QtCore/QtGlobal>

#include "tile.h"

namespace mjn {

   class Game;

   /**
    * @brief Mahjong player
    *
    * @todo Document this class!!!
    */
   class ServerPlayer: public QObject {
      Q_OBJECT;
      public:
         /// Creates player connected to specified session and game.
         ServerPlayer(Game* game);
         ~ServerPlayer() {};

         /// @return @b true if a tile have already been taken in the current turn.
         bool tileTaken() const {return mTileTaken;}

         /// Returns player wind
         WindType playerWind() const {return mPlayerWind;}
         /// Sete player wind
         void setPlayerWind(WindType value) {mPlayerWind = value;}

         /// Returns player's score
         int getScore() const {return mPlayerScore;}

         /// Returns hand size.
         int getHandSize() const {return mHand.size();}
         /// Returns const referance to the hand
         const QList<TileID>& getHand() const {return mHand;}
         /// Returns network session

         /// Makes necessary preparations on the new round begin.
         void beginRound();

         /**
          * @return @b true if this player start making his turn
          */
         bool isMyTurn() const {return mMyTurn;}

         /// Request player to make turn
         void makeTurn(bool firstTurn = false);

         /// Returns player name
         const QString &name() const {return mName;}

      public slots:
         /// Discard a tile and finish current turn.
         void endTurn(mjn::TileID discardedTile);
         /// Ask player to take a tile from the wall.
         void takeTileFromWall();
         /// Ask player to take the discarded tile.
         void takeDiscardedTile();
         /// Sets player name
         void setName(const QString &name);
      signals:
         /**
          * This signal is emitted in the beggining of each new round.
          *
          * @param myWind player wind in current round
          * @param gameWind current game prevailing wind
          * @param myHand players' hand at the beggining of round
          */
         void startOfRound(mjn::WindType myWind, mjn::WindType gameWind,QList<mjn::TileID> myHand);
         /// This signal is emitted in the beggining of this players' turn
         void startOfTurn();
         /**
          * This signal is emitted each time when player takes a tile.
          *
          * @param takenTile ID of the tile that was taken.
          */
         void tileTaken(mjn::TileID takenTile);
         /**
          * This signal is emitted if client requst to perform some operation
          * which can't be made. This situation can be caused only by bug
          * in client app.
          */
         void riseError(QString description);

         /**
          * Rised when information which should be available for other players
          * changed.
          */
         void updated();
      private:
         Q_DISABLE_COPY(ServerPlayer);
         QString mName;
         WindType mPlayerWind;
         int mPlayerScore;
         Game* mGame;
         QList<TileID> mHand;
         /** @brief Tile taken flag
          *
          * Player must take one tile from the wall (or the last discarded tile,
          * if palyer needs this tile to group chou) during a turn. This flag is
          * raised when tile is taken to prevent taking more than one tile.
          */
         bool mTileTaken;
         bool mMyTurn;
   };

} // namespace mjn

#endif // _Player_H_
